package com.jcsz.gulimall.product.app;

import java.util.Arrays;
import java.util.List;

import com.jcsz.gulimall.product.service.BrandService;
import com.jcsz.gulimall.product.service.CategoryService;
import com.jcsz.gulimall.product.vo.BrandVo;
import com.jcsz.gulimall.product.vo.CategoryBrandRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jcsz.gulimall.product.entity.CategoryBrandRelationEntity;
import com.jcsz.gulimall.product.service.CategoryBrandRelationService;
import com.jcsz.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author 一把Po吉他
 * @email volibear@gulimall.com
 * @date 2022-10-16 08:40:06
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;


    /**
     *  /product/categorybrandrelation/brands/list
     */
    @GetMapping("/brands/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R brandslist(@RequestParam Long catId){
        List<BrandVo> brandVos = categoryBrandRelationService.getRelationByCatId(catId);

        return R.ok().put("data", brandVos);
    }
    /**
     * 列表
     */
    @GetMapping("/catelog/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Long brandId){
        List<CategoryBrandRelationVo> categoryBrandRelationVos = categoryBrandRelationService.getRelationByBrandId(brandId);

        return R.ok().put("data", categoryBrandRelationVos);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        categoryBrandRelation.setCatelogName( categoryService.getById(categoryBrandRelation.getCatelogId()).getName());
        categoryBrandRelation.setBrandName(brandService.getById(categoryBrandRelation.getBrandId()).getName());
        categoryBrandRelationService.save(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
